package com.jrtc.backend.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jrtc.base.entity.bo.Disease;
import com.jrtc.base.config.dto.ResultDTO;
import com.jrtc.base.config.dto.ResultDTOBuilder;
import com.jrtc.base.entity.vo.DiseaseVo;
import com.jrtc.service.DiseaseService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 疾病表(Disease)表控制层
 *
 * @author makejava
 * @since 2022-03-21 17:19:02
 */
@RestController
@RequestMapping("disease")
public class DiseaseController {
    /**
     * 服务对象
     */
    @Resource
    private DiseaseService diseaseService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public Disease selectOne(Integer id) {
        return this.diseaseService.queryById(id);
    }
    
    @RequestMapping("getDiseaseList")
    public ResultDTO getDiseaseList(@RequestBody Disease disease)
    {
        IPage<Disease> ipage = diseaseService.queryDiseaseList(disease);
        return ResultDTOBuilder.success(ipage);
    }

    @RequestMapping("insertDisease")
    public ResultDTO insertDisease(@RequestBody Disease disease)
    {
        Disease tmpDisease = diseaseService.insert(disease);
        if (tmpDisease != null)
        {
           return ResultDTOBuilder.success("添加成功！");
        }
        return ResultDTOBuilder.failureMsg("添加失败！");
    }

    @RequestMapping("getDiseaseById")
    public ResultDTO getDiseaseById(@RequestBody Map<String, Integer> map)
    {
        Integer diseaseId = map.get("diseaseId");
        Disease disease = diseaseService.queryById(diseaseId);
        if (disease==null)
        {
            return ResultDTOBuilder.failureMsg("信息有误！");
        }
        return ResultDTOBuilder.success(disease);
    }

    @RequestMapping("updateDisease")
    public ResultDTO updateDisease(@RequestBody Disease disease)
    {
        if (disease==null)
        {
            return ResultDTOBuilder.failureMsg("信息有误！");
        }
        if (diseaseService.update(disease)!=null)
        {
            return ResultDTOBuilder.success("修改信息成功！");
        }
        return ResultDTOBuilder.failureMsg("修改信息失败！");
    }
    
    @RequestMapping("deleteDisease")
    public ResultDTO deleteDisease(@RequestBody Map<String,Integer> map)
    {
        int diseaseId = map.get("diseaseId");
        if (diseaseService.deleteById(diseaseId))
        {
            return ResultDTOBuilder.success("删除成功！");
        }
        return ResultDTOBuilder.failureMsg("删除失败！");
    }

    /**
     * 查询疾病信息
     * */
    @PostMapping("/selectDisease")
    public ResultDTO selectDisease(@RequestBody Map<String, Object> map){
        Integer diseasegategoryId = null;
        if (map.get("diseasegategoryId") != null && !map.get("diseasegategoryId").equals("")){
            diseasegategoryId = Integer.parseInt(map.get("diseasegategoryId").toString());
        }
        List<DiseaseVo> diseaseVos;
        try {
            diseaseVos = diseaseService.selectDisease(diseasegategoryId);
        } catch (Exception e){
            return ResultDTOBuilder.failureMsg(e.getMessage());
        }
        if (diseaseVos != null && diseaseVos.size() > 0){
            return ResultDTOBuilder.success(diseaseVos);
        }
        else {
            return ResultDTOBuilder.failureMsg("未查到疾病信息");
        }
    }
}